package byj.com.meishi.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import byj.com.meishi.bean.Media;
import byj.com.meishi.bean.Movie;
import byj.com.meishi.bean.Title;

/**
 * Created by My on 2016/9/3.
 */
public class AnalysisJson {
    public static List<Title> analysisJsonData(String json){
        List<Title> list=new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id = jsonObject.getString("id");
                String name = jsonObject.getString("name");
                Title title=new Title(id,name);
                list.add(title);
            }

            return  list;

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static List<Movie> analysisRecyclerItemData(String json) {
        List<Movie> list=new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String type = jsonObject.getString("type");
                if(type.equals("scheme")){
                    String recommendCaption = jsonObject.getString("recommend_caption");
                    String recommendCoverPic = jsonObject.getString("recommend_cover_pic");
                    Movie movie = new Movie(recommendCaption, recommendCoverPic, type);
                    list.add(movie);
                }else if(type.equals("media")){
                    String recommendCaption = jsonObject.getString("recommend_caption");
                    String recommendCoverPic = jsonObject.getString("recommend_cover_pic");
                    JSONObject media = jsonObject.getJSONObject("media");
                    long id = media.getLong("id");
                    String caption = media.getString("caption");
                    String video = media.getString("video");
                    int likesCount = media.getInt("likes_count");
                    Media media1 = new Media(id, caption, video, likesCount);
                    Movie movie = new Movie(recommendCaption, recommendCoverPic, media1, type);
                    list.add(movie);
                }


            }

            return  list;

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static List<BaikePagerMovie> anaylysisBaikePagerJson(String json){
//        try {
//            List<BaikePagerMovie> list=new ArrayList<>();
//            JSONObject jsonObject = new JSONObject(json);
//            JSONArray jsonArray = jsonObject.getJSONArray("maxNotifyList");
//            for (int i = 0; i <jsonArray.length() ; i++) {
//                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
//                String name = jsonObject1.getString("name");
//                String logo1 = jsonObject1.getString("logo1");
//                String mobilePreview=jsonObject1.getString("mobilePreview");
//                BaikePagerMovie baikePagerMovie = new BaikePagerMovie(name, logo1, mobilePreview);
//                list.add(baikePagerMovie);
//            }
//            return list;
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return  null;
//    }

}
